package com.ailanyin.modules.system.controller;

import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.security.utils.SecurityUtil;
import com.ailanyin.modules.system.domain.SysMenu;
import com.ailanyin.modules.system.domain.vo.LoginBodyVo;
import com.ailanyin.modules.system.service.ISysLoginService;
import com.ailanyin.modules.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2022/7/24 0024 上午 11:08
 */
@RestController
@Api(tags = "登录")
public class SysLoginController {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysLoginService sysLoginService;

    @GetMapping("/captchaImage")
    @ApiOperation(value = "获取验证码")
    public AjaxResult getCaptchaImage() {
        return sysLoginService.getCaptchaImage();
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public AjaxResult login(@RequestBody LoginBodyVo loginBody) {
        return sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public AjaxResult register(@RequestBody LoginBodyVo register) {
        sysLoginService.register(register);
        return AjaxResult.success();
    }

    @GetMapping("/getRouters")
    @ApiOperation(value = "获取路由信息")
    public AjaxResult getRouters() {
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(SecurityUtil.getLoginUserId());
        return AjaxResult.success(sysMenuService.buildMenus(menus));
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "获取用户信息")
    public AjaxResult getInfo() {
        return sysLoginService.getInfo();
    }
}
